package com.moefor.clover.init.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
@Configuration
public class HibernateSessionFactoryConfig {
	@Autowired
	private DataSource dataSource;
	
	@Bean(name = "sessionFactory")
	public LocalSessionFactoryBean localSessionFactoryBean(){
		LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		Properties hibernateProperties = new Properties();
		try {
			InputStream inStream = this.getClass().getResourceAsStream("/config/hibernate.properties");
			hibernateProperties.load(inStream);
			inStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		sessionFactory.setHibernateProperties(hibernateProperties);
		sessionFactory.setPackagesToScan("com.moefor.clover.model");
		return sessionFactory;
	}
}
